package com.demo.core.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.core.cache.CacheKey;
import com.demo.core.cache.CacheWrapper;
import com.demo.core.dao.MemberDao;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.dao.impl.base.PageCoreDaoSupport;
import com.demo.core.model.Member;
import com.demo.core.model.support.GeneralExecuteModel;

/**
 * 会员数据访问层实现类
 * 
 * @author ryuu.kk
 */
public class MemberDaoImpl extends PageCoreDaoSupport<Member, Long> implements MemberDao {
	/**
	 * 会员空间 
	 */
	protected final static String MEMBER_SPACE = "memberSpace.";
	/**
	 * 模型表名
	 */
	protected final static String TABLE_NAME = "T_CORE_MEMBER";
	/**
	 * 模型表主键ID
	 */
	protected final static String TABLE_PK_NAME = "ID";
	
	@Override
	public int updateStatus(Member member, Map<String, Object> memberStatus) {
		cacheClear();
		memberStatus.put("member", member);
		return getSqlMapClientTemplate().update(nameSpace() + "changeMemberStatus", memberStatus);
	}
	
	@Override
	public int updateLast(Member member, Map<String, Object> memberInfo) {
		cacheClear();
		memberInfo.put("member", member);
		return getSqlMapClientTemplate().update(nameSpace() + "changeLastLogin", memberInfo);
	}
	
	@Override
	public int updateCurrent(Member member, Map<String, Object> memberInfo) {
		cacheClear();
		memberInfo.put("member", member);
		return getSqlMapClientTemplate().update(nameSpace() + "changeCurrentLogin", memberInfo);
	}
	
	/**
	 * 联合分页检索
	 * @return
	 */
	@Override
	public ResultSearchResult<Member> searchUnion(Long siteId, SearchArgumentSupport args) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		return search("query", params, args);
	}
	
	@Override
	public List<Member> queryList(Long userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		return query(params);
	}
	
	@Override
	public List<Member> queryList(String userName) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userName", userName);
		return query(params);
	}
	
	@Override
	public Member query(Long memberId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", memberId);
		List<Member> memberList = query(params);
		if (memberList.size() > 0) {
			return memberList.get(0);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	private List<Member> query(Map<String, Object> params) {
		CacheKey key = null;
		List<Member> result = null;
		CacheWrapper<CacheKey, Object> cache = null;
		
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			key.update("query");
			key.update(params.hashCode());
			result = (List<Member>) cache.get(key);
		}
		
		if (result == null) {
			result  = getSqlMapClientTemplate().queryForList(nameSpace() + "query", params);
			if (isUseCache() && cache != null && result != null) {
				cache.put(key, result);
			}
		}
		return result;
	}

	@Override
	protected String nameSpace() {
		return MEMBER_SPACE;
	}
	
	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setPk(TABLE_PK_NAME);
		model.setTable(TABLE_NAME);
	}
}
